package com.example.juc;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.TimeUnit;

public class ConcurrentTest {
    public static void main(String[] args) {
//        ConcurrentMap<String, Object> concurrentHashMap = new ConcurrentHashMap<>();
        Map<String, Object> concurrentHashMap = Collections.synchronizedMap(new HashMap<String, Object>());
        new Thread(()->{
            for (Integer i = 0; i < 10; i++) {
                concurrentHashMap.put(i.toString(),i);
            }
        }).start();

        new Thread(()->{
            for (Integer i = 10; i < 20; i++) {
                concurrentHashMap.put(i.toString(),i);
            }
        }).start();

        new Thread(()->{
            for (Integer i = 20; i < 30; i++) {
                concurrentHashMap.put(i.toString(),i);
            }
        }).start();
        try {
            TimeUnit.SECONDS.sleep(1);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        Set<String> strings = concurrentHashMap.keySet();
        for (String string : strings) {
            System.out.println(concurrentHashMap.get(string));
        }
    }
}
